var express = require('express');
var router = express.Router();
const service = require('../service/service');

//查询所有
router.get('/', async function (req, res) {
    let { type, value, current, size } = req.query;
    console.log(req.query);
    let searchObj = {};//
    if (type) {
        if (type == "number") {
            searchObj[type] = value;
        } else {
            searchObj[type] = { $regex: value };
        }
    }
    let data = await service.findService(searchObj, { current: current || 1, size: size || 10 });
    res.send(data);

})
// 新增服务
router.post('/', async function (req, res) {
    // console.log('新增服务', req.body);
    let data = await service.insert(req.body);
    res.send(data);
})
// 删除服务
router.delete('/:id', async function (req, res) {
    let id = req.params.id;
    let data = await service.delete(id);
    res.send(data);
})
// 通过服务的id查询
router.get('/:id', async function (req, res) {
    let id = req.params.id;
    let data = await service.findById(id);
    res.send(data);
})
// 修改服务内容
router.put('/:id', async function (req, res) {
    let id = req.params.id;
    if(req.body.screens){
        req.body.screens = JSON.parse(req.body.screens);
    }
    let data = await service.update(id,req.body);
    res.send(data);
})
module.exports = router;